package org.dtrd.modules.device.entity.bean.bioland;


import lombok.Data;
import org.dtrd.modules.device.config.DeviceConstant;

import java.io.Serializable;


/***************************************************************************
 * <PRE>
 *
 *  Project Name    : cdms-api
 *
 *  Description     : 血糖/血压设备数据采集数据报文对象
 *
 *  Author          : huke.zhang
 *
 * </PRE>
 ***************************************************************************/
@Data
public class SrcDataValue implements Serializable {

    private static final long serialVersionUID = -1203493109929647895L;

    private String srcData;
    private String start;
    private String version;
    private String user;
    private String customer;
    private String modelCode;
    private String deviceType;
    private String serialNumber;
    //如果是血压 data1  高压 data2 低压
    private String highBloodPressure;
    private String lowBloodPressure;
    /**
     * 血糖仪是以mg/dL为单位传送，如果要显示为mmol/dL的单位，则要除以18，并四舍五入才能与血糖仪端显示的数据一致
     */
    private String sugarValue;
    /**
     * 心率
     */
    private int pulse;
    /**
     * 仅年份后两位 需补齐
     */
    private String year;
    private String month;
    private String day;
    private String hour;
    private String minute;
    private String checkCode;
    private String separator43;
    private String imsi;
    private String separator59;
    private String imei;
    private String crcCode;

    private static final String YEAR_PREFFIX = "20";

    public SrcDataValue(String srcdata) {
        this.srcData = srcdata;
        parseData();
    }

    protected void parseData() {

        this.start = this.srcData.substring(0, 2);
        this.version = this.srcData.substring(2, 4);
        this.user = this.srcData.substring(4, 5);
        this.customer = this.srcData.substring(5, 7);
        this.modelCode = this.srcData.substring(7, 9);
        // 机种码
        this.deviceType = this.srcData.substring(9, 11);
        this.serialNumber = this.srcData.substring(11, 20);
        this.highBloodPressure = this.srcData.substring(20, 23);
        this.lowBloodPressure = this.srcData.substring(23, 26);
        this.year = "20" + this.srcData.substring(29, 31);
        String tmpValue = this.srcData.substring(26, 29);
        if (isBloodPressure()) {
            this.pulse = Integer.valueOf(tmpValue);
        } else {
            this.sugarValue = tmpValue;
        }

        this.month = this.srcData.substring(31, 33);
        this.day = this.srcData.substring(33, 35);
        this.hour = this.srcData.substring(35, 37);
        this.minute = this.srcData.substring(37, 39);
        this.checkCode = this.srcData.substring(39, 43);
        this.separator43 = this.srcData.substring(43, 44);
        this.imsi = this.srcData.substring(44, 59);
        this.separator59 = this.srcData.substring(59, 60);
        this.imei = this.srcData.substring(60, 75);

        if (this.srcData.length() > 85) {
            this.crcCode = this.srcData.substring(85);
        }
    }

    public String getCheckTime() {
        StringBuffer datesb = new StringBuffer(this.year).append("-");
        datesb.append(this.month).append("-").append(this.day).append(" ");
        datesb.append(this.hour).append(":").append(this.minute).append(":00");
        return datesb.toString();
    }

    public boolean isSugarRecord() {
        return DeviceConstant.DeviceCategory.isSugar(deviceType);
    }

    public boolean isBloodPressure() {
        return DeviceConstant.DeviceCategory.isBloodPressure(deviceType);
    }


}
